#pragma once
#include<iostream>
#include<assert.h>

namespace Test
{
	class string
	{
	public:

		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		string()
			://_str(nullptr)
			 _str(new char[1])
			, _size(0)
			, _capacity(0)
		{
			_str[0] = '\0';
		}

		string(const char *str)
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		string(const string&s)
		{
			_size = s._size;
			_capacity=_size;
			_str = new char[_capacity+1];
			strcpy(_str, s._str);
		}

		bool operator== (const string &s)
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator> (const string& s)
		{
			return strcmp(_str, s._str) > 0;
		}

		bool operator< (const string& s)
		{
			return !(_str == s._str || _str > s._str);
		}

		bool operator>= (const string& s)
		{
			return _str == s._str || _str > s._str;
		}

		bool operator<=  (const string& s)
		{
			return _str == s._str || _str < s._str;
		}

		void reserve(int capacity)
		{
			char* tmp = new char[capacity + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity *= 2;
		}

		string& operator+= (const char ch)
		{
			push_back(ch);
			return *this;
		}

		string operator+= (const char* str)
		{
			append(str);
			return *this;
		}

		void push_back(const char& ch)
		{
			while (_size + 1 > _capacity)
			{
				reserve(_capacity * 2);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			int len = strlen(str);
			while (_size + len > _capacity)
			{
				reserve(_capacity * 2);
			}
			while (*str != '\0')
			{
				_str[_size] = *str;
				_size++;
				str++;
			}
			_str[_size] = '\0';
		}

		void insert(int pos, char ch)
		{
			assert(pos < _size);
			int n = _size;
			if (_size + 1 > _capacity)
			{
				reserve(_capacity * 2);
			}
			while (n >= pos)
			{
				_str[n + 1] = _str[n];
				n--;
			}
			_str[pos] = ch;
			_size++;
			_str[_size] = '\0';
		}

		const char* c_str()
		{
			return _str;
		}

		char& operator[] (size_t pos)
		{
			return _str[pos];
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

	private:
	    char* _str;
		size_t _size;
		size_t _capacity;

		static size_t npos;
	};

	static size_t npos = -1;

	void test_string_1()
	{
		string s1;
		string s2("Hello world");
		std::cout << s1.c_str() << std::endl;
		std::cout << s2.c_str() << std::endl;
		
	}

	void test_string_2()
	{
		string s1("Hello world!");
		string s2(s1);
		std::cout << s1.c_str() << std::endl;
		std::cout << s2.c_str() << std::endl;
		int i = 0;
		while (s1[i] != '\0')
		{
			std::cout << s1[i] << ' ';
			i++;
		}

	}

	void test_string_3()
	{
		string s1("asdfg");
		string s2("bsdfg");
		std::cout<< (s2 <= s1) << std::endl;
		std::cout << (s2 < s1) << std::endl;
		std::cout << (s2 > s1) << std::endl;
	}

	void test_string_4()
	{
		string s("Hello world!");
		string::iterator it = s.begin();
		while (*it != '\0')
		{
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
		for (auto ch: s)
		{
			std::cout << ch << " ";
		}

	 }

	void test_string_5()
	{
		string s1("Hello world!");
		string s2(s1);
		s1.push_back('*');
		s1.push_back('*');
		std::cout << s1.c_str()<< std::endl;
		s1 += '!';
		s1 += '!';
		std::cout << s1.c_str() << std::endl;
		s2.append("***");
		std::cout << s2.c_str() << std::endl;
		s2 += "!!!";
		std::cout << s2 .c_str() << std::endl;
		
	}

	void test_string_6()
	{
		string s1("Hello world!");
		s1.insert(5, '*');
		std::cout << s1.c_str() << std::endl;
		s1.insert(0, '*');
		std::cout << s1.c_str() << std::endl;

	}
}